package com.example.mapp.buildSql;

import org.apache.ibatis.jdbc.SQL;

import com.example.ext.entity.ExtSafeControlRecord;

/**
 * Created by douyx on 2018/03/26.
 * Updated by simon om 2018/08/09
 */
public class SafeControlRecordMappSql {

	public String queryList(ExtSafeControlRecord extSafeControlRecord) {
		String select = " distinct a.id as scrid,a.*, b.party_name as partyName,b.party_addr as partyAddr, b.corporation,b.contacts"
				+ ", b.contacts_work as contactsWork,b.contacts_phone as contactsPhone,pd.rspon_party_id as rsponPartyId,pd.rspon_party_name as rsponPartyName ";
		String table = " safe_control_record a left join party b on a.used_party_id = b.id "
				+ "left join plan_detail pd on a.p_detail_id=pd.id "
				+ "left join plan_dict pdt on pd.plan_dict_id=pdt.id "
				+ "left join party_user pu on pdt.p_party_id=pu.party_id "
				+ "left join sys_user su on pu.user_id=su.id and su.type='party' ";
		String whereParam = "";
		if (null != extSafeControlRecord.getTimeFrom() && !extSafeControlRecord.getTimeFrom().equals("")) {
			whereParam += " and a.check_time_to >= #{timeFrom}";
		}
		if (null != extSafeControlRecord.getTimeTo() && !extSafeControlRecord.getTimeTo().equals("")) {
			whereParam += " and a.check_time_to <= #{timeTo}";
		}
		if (null != extSafeControlRecord.getCheckType() && !extSafeControlRecord.getCheckType().equals("")) {
			whereParam += " and a.check_type = #{checkType}";
		}
		if (null != extSafeControlRecord.getCheckItem() && !extSafeControlRecord.getCheckItem().equals("")) {
			whereParam += " and a.check_item = #{checkItem}";
		}
		if (null != extSafeControlRecord.getDealWay() && !extSafeControlRecord.getDealWay().equals("")) {
			whereParam += " and a.deal_way = #{dealWay}";
		}
		if (null != extSafeControlRecord.getUsedPartyId() && !extSafeControlRecord.getUsedPartyId().equals("")) {
			whereParam += " and a.used_party_id = #{usedPartyId}";
		}
		if (null != extSafeControlRecord.getAdvicerId() && !extSafeControlRecord.getAdvicerId().equals("")) {
			whereParam += " and a.advicer_id = #{advicerId}";
		}
		if (null != extSafeControlRecord.getCheckerId() && !extSafeControlRecord.getCheckerId().equals("")) {
			whereParam += " and (a.checker_id = #{checkerId} or a.recorder_id=#{checkerId} or pdt.p_user_id=#{checkerId} or su.id=#{checkerId})";
		}
		if (null != extSafeControlRecord.getpDetailId() && !extSafeControlRecord.getpDetailId().equals("")) {
			whereParam += " and a.p_detail_id = #{pDetailId}";
		}
		if (null != extSafeControlRecord.getHasLawDocs() && !extSafeControlRecord.getHasLawDocs().equals("")) {
			if("1".equals(extSafeControlRecord.getHasLawDocs())){
				whereParam += " and a.law_docs is not null";
			}else{
				whereParam += " and a.law_docs is null";
			}
		}
		if (null != extSafeControlRecord.getHasPunishmentDocs() && !extSafeControlRecord.getHasPunishmentDocs().equals("")) {
			if("1".equals(extSafeControlRecord.getHasPunishmentDocs())){
				whereParam += " and a.punishment_docs is not null";
			}else{
				whereParam += " and a.punishment_docs is null";
			}
		}
		if (null != extSafeControlRecord.getPartyName() && !extSafeControlRecord.getPartyName().equals("")) {
			String partyName = "%"+extSafeControlRecord.getPartyName()+"%";
			whereParam += " and b.party_name like "+partyName;
		}
		if (null != extSafeControlRecord.getCensorType() && !extSafeControlRecord.getCensorType().equals("")) {
			whereParam += " and b.censor_type = #{censorType} ";
		}


		String where = " 1=1 " + whereParam;

		return new SQL() {
			{
				SELECT(select);
				FROM(table);
				WHERE(where);
				ORDER_BY(" a.check_time_to desc ");
			}
		}.toString();
	}
}
